﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.Configuration.Startup;
using Abp.Runtime.Session;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Magicodes.WeChat.Authorization;
using Magicodes.WeChat.User;
using Magicodes.WeChat.Web.Models.WeChatUsers;
using Magicodes.WeChat.WeChatApiSetting;
using Magicodes.WeChat.Web.Mvc.Models.WeChatApiSetting;
using Abp.Extensions;

namespace Magicodes.WeChat.Web.Controllers
{
    [AbpAuthorize(WeChatPermissions.Roots_Tenants_WeChatApiSetting)]
    public class WeChatApiSettingController : BaseController
    {
        private readonly IWeChatApiSettingAppService _weChatUserApiSettingAppService;
        private readonly IMultiTenancyConfig _multiTenancyConfig;
        public WeChatApiSettingController(
            IWeChatApiSettingAppService weChatUserApiSettingAppService,
            IMultiTenancyConfig multiTenancyConfig
        )
        {
            _weChatUserApiSettingAppService = weChatUserApiSettingAppService;
            _multiTenancyConfig = multiTenancyConfig;
        }


        public async Task<ActionResult> Index()
        {
            var output = await _weChatUserApiSettingAppService.GetAllSettingsAsync();
            ViewBag.IsMultiTenancyEnabled = _multiTenancyConfig.IsEnabled;
            var model = new SettingsViewModel
            {
                Settings = output.WeChatApiSetting,
                PaySettings = output.WeChatPayApiSetting
            };
            if (model.PaySettings.Notify.IsNullOrEmpty())
            {
                model.PaySettings.Notify =
                $"{Request.Scheme}://{Request.Host}/WeChatPayNotify/{(_multiTenancyConfig.IsEnabled ? AbpSession.GetTenantId().ToString() : string.Empty)}";
            }
            
            return View(model);
        }

        public ActionResult Test()
        {
            return View();
        }

    }
}